
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>后处理技术（下） - 浪子之心</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Blog,"> 
    <meta name="description" content="MrChen Bolg,HDRBloom（光晕）取亮光部分，进行降采样，在升采样的过程中，叠加降采样对应尺寸的图片，合并效果图和源图片
初处理如果放入的是HDR图片，可以在shader里面加float4 _MainTex_,"> 
    <meta name="author" content="Jack Chen"> 
    <link rel="alternative" href="atom.xml" title="浪子之心" type="application/atom+xml"> 
    <link rel="icon" href="/blog/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/blog/css/diaspora.css">

<meta name="generator" content="Hexo 5.4.1"><link rel="stylesheet" href="/blog/css/prism.css" type="text/css"></head>

<body class="loading">
    <span id="config-title" style="display:none">浪子之心</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://mrchenlearnspace.github.io/blog"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">后处理技术（下）</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">后处理技术（下）</h1>
        <div class="stuff">
            <span>二月 24, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/blog/tags/Shader/" rel="tag">Shader</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="HDRBloom（光晕）"><a href="#HDRBloom（光晕）" class="headerlink" title="HDRBloom（光晕）"></a>HDRBloom（光晕）</h1><p>取亮光部分，进行降采样，在升采样的过程中，叠加降采样对应尺寸的图片，合并效果图和源图片</p>
<h2 id="初处理"><a href="#初处理" class="headerlink" title="初处理"></a>初处理</h2><p>如果放入的是HDR图片，可以在shader里面加float4 _MainTex_HDR;   col.rgb  &#x3D; DecodeHDR(col,_MainTex_HDR);进行解码。</p>
<h2 id="取光亮部分"><a href="#取光亮部分" class="headerlink" title="取光亮部分"></a>取光亮部分</h2><p>先进行均值模糊，取亮色部分。</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">float</span> intensity = Mathf.Exp(_Intensity / <span class="number">10.0f</span> * <span class="number">0.693f</span>) - <span class="number">1.0f</span>;</span><br><span class="line"></span><br><span class="line">mat.SetFloat(<span class="string">&quot;_Threshold&quot;</span>, _Threshold);</span><br><span class="line">mat.SetFloat(<span class="string">&quot;_Intensity&quot;</span>, intensity);</span><br><span class="line"></span><br><span class="line"><span class="comment">//取亮色</span></span><br><span class="line">Graphics.Blit(source, RT_Down[<span class="number">0</span>], mat, <span class="number">0</span>);</span><br></pre></td></tr></table></figure>



<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">half4 <span class="title function_">frag_PreFilter</span><span class="params">(v2f_img i)</span> : SV_Target<span class="comment">//0</span></span><br><span class="line">&#123;</span><br><span class="line">    half4 col = tex2D(_MainTex, i.uv);</span><br><span class="line">    half4 d=_MainTex_TexelSize.xyxy*half4(<span class="number">1</span>,<span class="number">1</span>,<span class="number">-1</span>,<span class="number">-1</span>);</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.xy).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.zy).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.xw).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.zw).rgb;</span><br><span class="line">    col.rgb*=<span class="number">0.25</span>;</span><br><span class="line">    <span class="type">float</span> br = max(max(col.r,col.g),col.b);</span><br><span class="line">    br =  max(<span class="number">0</span>,(br - _Threshold))/max(<span class="number">0.00001</span>,br);</span><br><span class="line">    col.rgb*=br;</span><br><span class="line">    <span class="keyword">return</span> col;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="降采样"><a href="#降采样" class="headerlink" title="降采样"></a>降采样</h2><p>用双重盒状模糊的降采样方式</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//降采样</span></span><br><span class="line"><span class="keyword">for</span>(<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;_Iteration;i++) &#123;<span class="comment">//0-i-1</span></span><br><span class="line">    Graphics.Blit(RT_Down[i], RT_Down[i + <span class="number">1</span>], mat, <span class="number">1</span>);</span><br><span class="line">   <span class="comment">// print(&quot;downcode &quot; + i );</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">half4 <span class="title function_">frag_DownsampleBox</span><span class="params">(v2f_img i)</span> : SV_Target<span class="comment">//1</span></span><br><span class="line">&#123;</span><br><span class="line">    half4 col = tex2D(_MainTex, i.uv);</span><br><span class="line">    half4 d=_MainTex_TexelSize.xyxy*half4(<span class="number">1</span>,<span class="number">1</span>,<span class="number">-1</span>,<span class="number">-1</span>);</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.xy).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.zy).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.xw).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.zw).rgb;</span><br><span class="line">    col.rgb*=<span class="number">0.25</span>;</span><br><span class="line">    <span class="keyword">return</span> col;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="升采样"><a href="#升采样" class="headerlink" title="升采样"></a>升采样</h2><p>升采样过程中加同尺寸的降采样贴图</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Graphics.Blit(RT_Down[_Iteration], RT_Up[_Iteration]);</span><br><span class="line"><span class="keyword">for</span>(<span class="built_in">int</span> i=_Iteration;i&gt;<span class="number">0</span>;i--) &#123;</span><br><span class="line">    mat.SetTexture(<span class="string">&quot;_BloomTex&quot;</span>, RT_Down[i<span class="number">-1</span>]);</span><br><span class="line">    Graphics.Blit(RT_Up[i], RT_Up[i<span class="number">-1</span>], mat, <span class="number">2</span>);</span><br><span class="line">    <span class="comment">//print(&quot;upcode &quot; + i);</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">half4 <span class="title function_">frag_UpsampleBox</span><span class="params">(v2f_img i)</span> : SV_Target<span class="comment">//2</span></span><br><span class="line">&#123;</span><br><span class="line">    half4 col = tex2D(_MainTex, i.uv);</span><br><span class="line">    half4 d=_MainTex_TexelSize.xyxy*half4(<span class="number">1</span>,<span class="number">1</span>,<span class="number">-1</span>,<span class="number">-1</span>);</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.xy).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.zy).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.xw).rgb;</span><br><span class="line">    col.rgb+=tex2D(_MainTex,i.uv+d.zw).rgb;</span><br><span class="line">    col.rgb*=<span class="number">0.25</span>;</span><br><span class="line">    half4 color2 = tex2D(_BloomTex, i.uv);</span><br><span class="line">    <span class="keyword">return</span> col + color2;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="合并"><a href="#合并" class="headerlink" title="合并"></a>合并</h2><p>把升采样的最后一张图和原图合并,和释放贴图。</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">mat.SetTexture(<span class="string">&quot;_BloomTex&quot;</span>, RT_Up[<span class="number">0</span>]);</span><br><span class="line">Graphics.Blit(source, destination, mat, <span class="number">3</span>);</span><br><span class="line"><span class="comment">//Release</span></span><br><span class="line"><span class="comment">//RenderTexture.ReleaseTemporary(RT1);</span></span><br><span class="line"><span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration+<span class="number">1</span>; i++) &#123;</span><br><span class="line">   <span class="comment">// print(&quot;Release &quot; + i);</span></span><br><span class="line"></span><br><span class="line">    RenderTexture.ReleaseTemporary(RT_Down[i]);</span><br><span class="line">    RenderTexture.ReleaseTemporary(RT_Up[i]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">half4 <span class="title function_">frag_Combine</span><span class="params">(v2f_img i)</span> : SV_Target<span class="comment">//3</span></span><br><span class="line">&#123;</span><br><span class="line">    half4 col = tex2D(_MainTex, i.uv);</span><br><span class="line">    half3 color1 = tex2D(_BloomTex, i.uv).rgb;</span><br><span class="line">    col.rgb+= color1.rgb*_Intensity;</span><br><span class="line">    <span class="keyword">return</span> col ;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>



<h2 id="最后小瑕疵"><a href="#最后小瑕疵" class="headerlink" title="最后小瑕疵"></a>最后小瑕疵</h2><p>在光晕的周围还有点不自然。可用如下解决方案。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">float3 <span class="title function_">ACES_Tonemapping</span><span class="params">(float3 x)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="type">float</span> a = <span class="number">2.51f</span>;</span><br><span class="line">  <span class="type">float</span> b = <span class="number">0.03f</span>;</span><br><span class="line">  <span class="type">float</span> c = <span class="number">2.43f</span>;</span><br><span class="line">  <span class="type">float</span> d = <span class="number">0.59f</span>;</span><br><span class="line">  <span class="type">float</span> e = <span class="number">0.14f</span>;</span><br><span class="line">  float3 encode_color = saturate((x*(a*x + b)) / (x*(c*x + d) + e));</span><br><span class="line">  <span class="keyword">return</span> encode_color;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">half4 <span class="title function_">frag</span> <span class="params">(v2f_img i)</span> : SV_Target</span><br><span class="line">&#123;</span><br><span class="line">  half4 col = tex2D(_MainTex, i.uv);</span><br><span class="line">  half3 linear_color = <span class="built_in">pow</span>(col.rgb, <span class="number">2.2</span>);</span><br><span class="line">  half3 encode_color = ACES_Tonemapping(linear_color);</span><br><span class="line">  half3 final_color = <span class="built_in">pow</span>(encode_color, <span class="number">1.0</span> / <span class="number">2.2</span>);</span><br><span class="line">  <span class="keyword">return</span> float4(final_color,col.a);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="后处理的顺序"><a href="#后处理的顺序" class="headerlink" title="后处理的顺序"></a>后处理的顺序</h1><p><img src="https://img-blog.csdnimg.cn/3c671e3fee7c4999bd942e25acf1ea62.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUEMgRXJyb3I=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<p>调整顺序：Bloom-&gt;Vignette-&gt;Tonemapping-&gt;ColorGrading</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="/music/jucilang.mp3">
            </audio>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='99f95a936c6fcdb1bd67'
        data-cs='c2d1bba3d6b7f9ade4f3de09a63925bd837c25ea'
        data-r='mrchenlearnspace.github.io'
        data-o='MrChenLearnSpace'
        data-a='MrChenLearnSpace'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#HDRBloom%EF%BC%88%E5%85%89%E6%99%95%EF%BC%89"><span class="toc-number">1.</span> <span class="toc-text">HDRBloom（光晕）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9D%E5%A4%84%E7%90%86"><span class="toc-number">1.1.</span> <span class="toc-text">初处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%96%E5%85%89%E4%BA%AE%E9%83%A8%E5%88%86"><span class="toc-number">1.2.</span> <span class="toc-text">取光亮部分</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%99%8D%E9%87%87%E6%A0%B7"><span class="toc-number">1.3.</span> <span class="toc-text">降采样</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%87%E9%87%87%E6%A0%B7"><span class="toc-number">1.4.</span> <span class="toc-text">升采样</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%88%E5%B9%B6"><span class="toc-number">1.5.</span> <span class="toc-text">合并</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%80%E5%90%8E%E5%B0%8F%E7%91%95%E7%96%B5"><span class="toc-number">1.6.</span> <span class="toc-text">最后小瑕疵</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%90%8E%E5%A4%84%E7%90%86%E7%9A%84%E9%A1%BA%E5%BA%8F"><span class="toc-number">2.</span> <span class="toc-text">后处理的顺序</span></a></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/blog/js/plugin.js"></script>
<script src="/blog/js/typed.js"></script>
<script src="/blog/js/diaspora.js"></script>


<link rel="stylesheet" href="/blog/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/blog/photoswipe/default-skin/default-skin.css">


<script src="/blog/photoswipe/photoswipe.min.js"></script>
<script src="/blog/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>



<!-- Google Analytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YMJ8CBH8F7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-YMJ8CBH8F7');
</script>
<!-- End Google Analytics -->


</html>
